package com.esri.viewer
{
	//arcgis For Flex相关引用
	import com.esri.ags.Graphic;
	import com.esri.ags.events.DrawEvent;
	import com.esri.ags.geometry.*;
	import com.esri.ags.layers.GraphicsLayer;
	import com.esri.ags.tools.DrawTool;
	
	import comValue.comValue;
	
	import flash.events.ContextMenuEvent;
	import flash.external.ExternalInterface;
	import flash.ui.*;


	
	public class JavaScriptFlex extends BaseWidget
	{
		private var graphicsLayer:GraphicsLayer;	
	
		public function JavaScriptFlex()
		{
			super();
			
			//初始化动态绘制图层
			graphicsLayer = new GraphicsLayer();
			graphicsLayer.name = "Draw Features";
			//map.addLayer(graphicsLayer);
			
			ExternalInterface.addCallback("setMapNavigation",setNavigation);
			//ExternalInterface.addCallback("setMapAction",setAction);
			
			//打开对应的widget
			ExternalInterface.addCallback("openWidget",openWidget);
			
		}
		/**
		 * 打开相应的widget
		 * */
		public function openWidget(widgetId:String,widgetChildId:String,widgetChildName:String):void
		{
			comValue.comValue.currentWidgetID=int(widgetId);
			comValue.comValue.initSelectIndex=int(widgetChildId);
			comValue.comValue.widgetChildName=widgetChildName;
			
			ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_RUN,Number(widgetId)));		
				var infodata:Object={widgetId:widgetId,
					widgetChildId:widgetChildId,
					widgetChildName:widgetChildName
				};
			ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_CHILD_CHANGED,infodata));
		
		}
	/**
     * <p>The navigation methods supported are:</p>
     * <listing>
     * pan          (Navigation.PAN)
     * zoomin       (Navigation.ZOOM_IN)
     * zoomout      (Navigation.ZOOM_OUT)
     * zoomfull     (SiteContainer.NAVIGATION_ZOOM_FULL)
     * zoomprevious (SiteContainer.NAVIGATION_ZOOM_PREVIOUS)
     * zoomnext     (SiteContainer.NAVIGATION_ZOOM_NEXT)
     * </listing>
     *
     * <p>The navigation request can be sent as such:</P>
     * <listing>
     *  var data:Object =
     *     {
     *       tool: Navigation.PAN,
     *       status: status
     *      }
		 * 注册javascript调用地图基本操作功能函数
		 * */
		private function setNavigation(s_Navigation:String):void
		{
			//getDefaultString("");
			var data:Object={
				tool:s_Navigation,
				status:getDefaultString(s_Navigation)
			}
			ViewerContainer.dispatchEvent(new AppEvent(AppEvent.SET_MAP_NAVIGATION,data));
		}

	/**
	 * 删除已绘制图形
	 * */
	private function onContextMenuDelete(event:ContextMenuEvent):void
	{
		graphicsLayer.remove(Graphic(event.contextMenuOwner));
		// also remove measurement label if any
		graphicsLayer.remove(getMeasurementGraphic(Graphic(event.contextMenuOwner)));
	}
	private function getMeasurementGraphic(graphic:Graphic):Graphic
	{
		var result:Graphic;
		for (var i:int = 0; i < graphicsLayer.numGraphics; )
		{
			if (Graphic(graphicsLayer.getChildAt(i)).name == graphic.id)
			{
				result = Graphic(graphicsLayer.getChildAt(i));
				break;
			}
			else
			{
				i++;
			}
		}
		return result;
	}
	/**
	 * 调用底层arcgis For flex Api绘制图形
	 * */
	private function drawEnd(event:DrawEvent):void
	{
		// deactivate the draw tool
		event.target.deactivate();
		var geom:Geometry = event.graphic.geometry;
		
		var lastDrawnGraphic:Graphic= new Graphic(geom);
		lastDrawnGraphic.autoMoveToTop = false;
		
		var customContextMenu:ContextMenu = new ContextMenu();
		customContextMenu.hideBuiltInItems();
		var menuItem:ContextMenuItem = new ContextMenuItem("删除");
		menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onContextMenuDelete);
		customContextMenu.customItems.push(menuItem);
		
		lastDrawnGraphic.contextMenu = customContextMenu;
		
//		lastDrawnGraphic.addEventListener(MouseEvent.CLICK, graphicClickHandler);
//		switch (geom.type)
//		{
//			case Geometry.MAPPOINT:
//			{
//				if (drawMode == TEXT_MODE)
//				{
//					var textSymbol:TextSymbol = new TextSymbol(txtLabel.text);
//					var txtFormat:TextFormat = new TextFormat(textFont.selectedItem.font, numTextSize.value, cpText.selectedColor, bold.selected, italic.selected, underline.selected);
//					textSymbol.textFormat = txtFormat;
//					lastDrawnGraphic.symbol = textSymbol;
//				}
//				else
//				{
//					lastDrawnGraphic.symbol = new SimpleMarkerSymbol(markerStyle.selectedItem.style, numMarkerSize.value, cpMarker.selectedColor, numMarkerAlpha.value, 0, 0, 0, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, cpMarkerOutline.selectedColor, 1, numMarkerOutlineWidth.value));
//				}
//				break;
//			}
//			case Geometry.POLYLINE:
//			{
//				lastDrawnGraphic.symbol = new SimpleLineSymbol(lineStyle.selectedItem.style, cpLine.selectedColor, numLineAlpha.value, numLineWidth.value);
//				if (chkMeasurements.selected)
//				{
//					geometryService.project([ geom ], outSR);
//					var polyline:Polyline = geom as Polyline;
//					measurePt = polyline.extent.center;
//				}
//				break;
//			}
//			case Geometry.POLYGON:
//			{
//				lastDrawnGraphic.symbol = new SimpleFillSymbol(fillStyle.selectedItem.style, cpFill.selectedColor, numFillAlpha.value, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, cpFillOutline.selectedColor, 1, numFillOutlineWidth.value));
//				if (chkMeasurements.selected)
//				{						
//					if(GeometryUtil.polygonSelfIntersecting(Polygon(geom)))
//					{
//						geometryService.simplify([geom]);
//					}
//					else
//					{
//						geometryService.project([ geom ], outSR);
//						var polygon:Polygon = geom as Polygon;
//						measurePt = polygon.extent.center;
//					}
//				}
//				break;
//			}
//			case Geometry.EXTENT:
//			{
//				lastDrawnGraphic.symbol = new SimpleFillSymbol(fillStyle.selectedItem.style, cpFill.selectedColor, numFillAlpha.value, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, cpFillOutline.selectedColor, 1, numFillOutlineWidth.value));
//				if (chkMeasurements.selected)
//				{				
//					geometryService.project([ geom ], outSR);
//					var extent:Extent = geom as Extent;
//					measurePt = extent.center;				
//				}
//				break;
//			}
//		}
//		graphicsLayer.add(lastDrawnGraphic);
	}
	}
}